package com.ycsj.goldmedalnewconcept.fragment;


import io.rong.imkit.RongIM;
import io.rong.imkit.model.UIConversation;
import io.rong.imkit.widget.adapter.ConversationListAdapter;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ycsj.goldmedalnewconcept.App;
import com.ycsj.goldmedalnewconcept.DemoContext;
import com.ycsj.goldmedalnewconcept.R;

/**
 * 首页 设置 fragment
 *
 */
public class ＭessageNotLoginFragment extends BaseFragment   {

	private static final String TAG = "ＭessageNotLoginFragment";
	private Activity mActivity;
	private Object mConversitionListFragment;
	private FragmentManager mFragmentManager;
	private RelativeLayout mBack;
	private TextView mTitle;

	public static ＭessageNotLoginFragment newInstance() {
		ＭessageNotLoginFragment settingFragment = new ＭessageNotLoginFragment();

		return settingFragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.mActivity = activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_message_notlogin, container,
				false);
		
		return view;
	}

	
	
	
	
	
	

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initViews(view);
		initEvents();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	private void initViews(View view) {
		
	}
	
	private void initEvents() {
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	public String getFragmentName() {
		return TAG;
	}


	
	 /**
     * 加载 会话列表 ConversationListFragment
     */
    @SuppressLint("NewApi")
	private void enterFragment() {

      //  ConversationListFragment fragment = (ConversationListFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.conversationlist);

//        Uri uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
//                .appendPath("conversationlist")
//                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话非聚合显示
//                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "true")//设置群组会话聚合显示
//                .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")//设置讨论组会话非聚合显示
//                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")//设置系统会话非聚合显示
//                .build();
//
//        fragment.setUri(uri);
        
      
        
    }

    /**
     * 设置 actionbar 事件
     * @param view 
     */
    private void setActionBarTitle(View view) {

        mTitle = (TextView) view.findViewById(R.id.tv_title);
        mBack = (RelativeLayout) view.findViewById(R.id.rl_back);

        mTitle.setText("消息");
        mBack.setVisibility(View.GONE);
//        mBack.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//            	Toast.makeText(getActivity(), TAG, 0).show();
//            }
//        });

    }

    /**
     * 判断消息是否是 push 消息
     *
     */
    private void isReconnect() {

        Intent intent = getActivity().getIntent();
        String token ="B36pCG11LImqEpu4n70Wei75KxfWYG+yQTUevpxn5RVnTJFeDFjtvBZ4WzzY4UzQxUIm6aasunSb1ACB8HWmS14dO/zhgmzS";

        if (DemoContext.getInstance() != null) {

          //  token = DemoContext.getInstance().getSharedPreferences().getString("DEMO_TOKEN", "default");
        }

        //push，通知或新消息过来
        if (intent != null && intent.getData() != null && intent.getData().getScheme().equals("rong")) {

            //通过intent.getData().getQueryParameter("push") 为true，判断是否是push消息
            if (intent.getData().getQueryParameter("push") != null
                    && intent.getData().getQueryParameter("push").equals("true")) {

                reconnect(token);
            } else {
                //程序切到后台，收到消息后点击进入,会执行这里
                if (RongIM.getInstance() == null || RongIM.getInstance().getRongIMClient() == null) {

                    reconnect(token);
                } else {
                    enterFragment();
                }
            }
        }
    }

    /**
     * 重连
     *
     * @param token
     */
    @SuppressLint("NewApi")
	private void reconnect(String token) {

        if (getActivity().getApplicationInfo().packageName.equals(App.getCurProcessName(getActivity().getApplicationContext()))) {

            RongIM.connect(token, new RongIMClient.ConnectCallback() {
                @Override
                public void onTokenIncorrect() {

                }

                @Override
                public void onSuccess(String s) {

                    enterFragment();
                }

                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {

                }
            });
        }
    }
	
    public class ConversationListAdapterEx extends ConversationListAdapter {
        public ConversationListAdapterEx(Context context) {
            super(context);
        }

        @Override
        protected View newView(Context context, int position, ViewGroup group) {
        	
            return super.newView(context, position, group);
        }

        @Override
        protected void bindView(View v, int position, UIConversation data) {
        if(data.getConversationType().equals(Conversation.ConversationType.DISCUSSION))
            data.setUnreadType(UIConversation.UnreadRemindType.REMIND_ONLY);
            super.bindView(v, position, data);
        }
    }
}
